package cn.clwxy.gdz.service;

import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.clwxy.gdz.bo.PersonalCenterInfoBean;
import cn.clwxy.gdz.entity.TeacherOrderNotPay;
import cn.clwxy.gdz.entity.TeacherOrderSuccess;
import cn.clwxy.gdz.entity.TeacherOrderTotal;
import cn.clwxy.gdz.util.BaseDao;

public class PersonalCenterInfoActServiceImpl extends BaseDao implements
		PersonalCenterInfoActService {

	@Override
	public PersonalCenterInfoBean getTeacherInfo(Integer teacherId) {
		String sql = "SELECT teacherUserName,t.teachYear,t.teacherId,teacherLevel,teacherNative,teacherRegisterDate,GROUP_CONCAT(subjectName) subjectName, "
				+ " clicks,teacherUpdateDate,teacherPwd,teacherRealName,certificate,ii.src,"
				+ " teacherSex,teacherEducation,teacherUniversity,t.workType"
				+ " teacherSpeciality,selfmark,teacherPhone,teacherIdentity,tt.type type "
				+ " FROM teacher t "
				+ " left JOIN teachertype tt ON t.teacherType=tt.id "
				+ " left JOIN teachersubjectrelative tsr ON tsr.teacherId=t.teacherId "
				+ " left JOIN `subject` sj ON sj.subjectId=tsr.subjectId "
				+ " left JOIN teacheraddress ta ON t.teacherId=ta.teacherId "
				+ " left JOIN addresscounty ac ON ac.addressId=ta.addressCountryId "
				+ " left JOIN imgid ii ON ii.id=t.teacherImgId "
				+ " WHERE t.teacherId=?  GROUP BY teacherId ";
		return super.querySingle(sql, new BeanHandler<PersonalCenterInfoBean>(
				PersonalCenterInfoBean.class), teacherId);
	}

	@Override
	public List<TeacherOrderNotPay> getUnpayOrders(Integer teacherId) {
		String sql = "SELECT o.orderId,o.price,o.orderBeginTime,o.teacherId,GROUP_CONCAT(sj.subjectName) subjectName,ac.addressName addressName,o.isPay "
				+ " FROM `order`o "
				+ " left JOIN student s ON s.studentId=o.studentId "
				+ " left JOIN ordersubjectrelative osr ON osr.orderId=o.orderId "
				+ " left JOIN `subject` sj ON sj.subjectId=osr.subjectId "
				+ " left JOIN studentadderss sa ON sa.studentId=s.studentId "
				+ " left JOIN addresscounty ac ON ac.addressId=sa.addressCountyId "
				+ " WHERE o.teacherId=? and o.isPay=0 GROUP BY orderId";
		return super.queryList(sql, new BeanListHandler<TeacherOrderNotPay>(
				TeacherOrderNotPay.class), teacherId);
	}

	@Override
	public List<TeacherOrderTotal> getTotalOrders(Integer teacherId) {
		String sql = "SELECT o.orderId,o.orderBeginTime,o.teacherId,o.isPushToIndex,GROUP_CONCAT(sj.subjectName) subjectName,orderFinishTime,price"
				+ " FROM `order`o "
				+ " left JOIN student s ON s.studentId=o.studentId "
				+ " left JOIN studentsubjectrrelative ssr ON ssr.studentId=s.studentId "
				+ " left JOIN `subject` sj ON sj.subjectId=ssr.subjectId "
				+ " WHERE o.teacherId=? GROUP BY orderId";
		return super.queryList(sql, new BeanListHandler<TeacherOrderTotal>(
				TeacherOrderTotal.class), teacherId);
	}

	@Override
	public List<TeacherOrderSuccess> getSuccessOrders(Integer teacherId) {
		String sql = "SELECT o.orderId,o.orderBeginTime,o.studentId,o.teacherId,GROUP_CONCAT(sj.subjectName) subjectName,orderFinishTime,o.price,o.isPay "
				+ " FROM `order`o "
				+ " left JOIN student s ON s.studentId=o.studentId "
				+ " left JOIN studentsubjectrrelative ssr ON ssr.studentId=s.studentId "
				+ " left JOIN `subject` sj ON sj.subjectId=ssr.subjectId "
				+ " WHERE o.teacherId=? and  o.isPay=1 GROUP BY orderId ";
		return super.queryList(sql, new BeanListHandler<TeacherOrderSuccess>(
				TeacherOrderSuccess.class), teacherId);
	}

	@Override
	public int updatePwd(Integer teacherId, String oldPwd, String newPwd) {
		String sql = "UPDATE teacher SET teacherPwd=? WHERE teacherId=? AND teacherPwd=?";
		return super.update(sql,newPwd, teacherId, oldPwd );
	}

	@Override
	public int updateInfo(PersonalCenterInfoBean personalCenterInfoBean) {
		String sql = "UPDATE teacher t "
				+ " INNER JOIN teachertype tt ON t.teacherType=tt.id "
				+ " INNER JOIN teachersubjectrelative tsr ON tsr.teacherId=t.teacherId "
				+ " INNER JOIN `subject` sj ON sj.subjectId=tsr.subjectId"
				+ " INNER JOIN teacheraddress ta ON t.teacherId=ta.teacherId"
				+ " INNER JOIN addresscounty ac ON ac.addressId=ta.addressCountryId"
				
				+ " SET addressDesc=?,addressName=?, teacherSex=?,"
				+ " teacherRealName=?,certificate=?, "
				+ " teacherUniversity=?,"
				+ " teacherSpeciality=?,selfmark=?,teacherPhone=?"
				+ " where t.teacherId=?";
		System.out.println(personalCenterInfoBean.getSelfmark());
		return super.update(sql, personalCenterInfoBean.getAddressDesc(),
				personalCenterInfoBean.getAddressName(),
				personalCenterInfoBean.getTeacherSex(),
				personalCenterInfoBean.getTeacherRealName(),
				personalCenterInfoBean.getCertificate(),
				personalCenterInfoBean.getTeacherUniversity(),
				personalCenterInfoBean.getTeacherSpeciality(),
				personalCenterInfoBean.getSelfmark(),
				personalCenterInfoBean.getTeacherPhone(),
				personalCenterInfoBean.getTeacherId());
	}

	@Override
	public int updateisDelete(Integer teacherId, String teacherPwd) {
		String sql="UPDATE teacher t SET isDelete =1 WHERE teacherId=? AND teacherPwd=?";
		return super.update(sql, teacherId,teacherPwd);
	}
	public int updateHeadImg(Integer id,String src){
		String sql="";
		return super.update(sql, id,src);
	}
	
/*	public static void main(String[] args) {
		PersonalCenterInfoBean ps=new PersonalCenterInfoBean();
		ps.setSubjectName("鍘嗗彶");
		ps.setAddressDesc("瀹夊痉闂ㄩ檮杩�");
		ps.setAddressName("闆ㄨ姳鍙板尯");
		ps.setCertificate("鑻辫鍏骇璇佷功锛岃绠楁満浜岀骇璇佷功");
		ps.setTeacherRealName("涓戒附");
		ps.setTeacherUniversity("娓呭崕澶у");
		ps.setTeacherSpeciality("鑻辫");
		ps.setType("鍦ㄦ牎瀛︾敓");
		ps.setSelfmark("寰堝ソ鐨�");
		ps.setTeacherPhone("18778822222");
		ps.setTeacherId(35);
		PersonalCenterInfoActService psc=new PersonalCenterInfoActServiceImpl();
		System.out.println(psc.updateInfo(ps));
		
	}*/
public static void main(String[] args) {
	PersonalCenterInfoActServiceImpl im= new PersonalCenterInfoActServiceImpl();
	List<TeacherOrderTotal> totalOrders = im.getTotalOrders(1);
	System.out.println(totalOrders);
}
}
